package com.xoa.service.cms;

import com.xoa.dao.portal.SiteTemplateMapper;
import com.xoa.dao.portalArticle.PortalArticleMapper;
import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.dao.portalTemplate.PortalTemplateMapper;
import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.portalArticle.PortalArticleWithBLOBs;
import com.xoa.model.portalColumn.PortalColumn;
import com.xoa.model.portalTemplate.PortalTemplate;
import com.xoa.model.cms.Site;
import com.xoa.util.cms.CMSTemplateUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 张勇 on 2018/3/14.
 */
@Service
public class RenderingService {

    @Autowired
    SiteMapper siteMapper;

    @Autowired
    SiteTemplateMapper siteTemplateMapper;

    @Autowired
    PortalColumnMapper portalColumnMapper;

    @Autowired
    PortalTemplateMapper portalTemplateMapper;

    @Autowired
    PortalArticleMapper portalArticleMapper;

    @Autowired
    RenderingService renderingService;

    /**
     * 渲染首页模板
     */
    public String renderSite(int siteId,Map param,boolean preview,String rootPath){
        //获取站点信息
        Site site = siteMapper.selectByPrimaryKey(siteId);
        //获取站点首页模板信息
        PortalTemplate portalTemplate = portalTemplateMapper.selectByPrimaryKey(site.getHomeTemplate());
        if(portalTemplate != null){
            //存在则拼接站点磁盘路径
            String path = rootPath + File.separator + "cmsTmp" + File.separator + portalTemplate.getPortalId();
            Configuration config = new Configuration();
            ByteArrayOutputStream outStream = null;
            Writer w = null;
            try{
                config.setDirectoryForTemplateLoading(new File(path));

                //把service添加到参数去
                //站点Service
                config.setCustomAttribute("siteMapper",siteMapper);
                //站点模板Service
                config.setCustomAttribute("portalTemplateMapper",portalTemplateMapper);
                //栏目Mapper
                config.setCustomAttribute("portalColumnMapper",portalColumnMapper);

                //文章Mapper
                config.setCustomAttribute("portalArticleMapper",portalArticleMapper);
                //渲染的Service
                config.setCustomAttribute("renderingService",renderingService);

                //附加参数
                config.setCustomAttribute("ROOT_PATH","/");
                config.setCustomAttribute("SITE_ID",site.getPortalId());

                //预览模式
                config.setCustomAttribute("preview",true);

                Template template = config.getTemplate(portalTemplate.getTemplateFile(),"UTF-8");
                outStream = new ByteArrayOutputStream();
                w = new OutputStreamWriter(outStream,"UTF-8");

                //设置参数
                    template.process(CMSTemplateUtil.getInstance(),w);
                return outStream.toString("UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }finally {
                if(outStream!=null){
                    try {
                        outStream.close();
                    } catch (IOException e) {
                    }
                }
                if(w!=null){
                    try {
                        w.close();
                    } catch (IOException e) {

                    }
                }
            }
        }

        return null;
    }

    /**
     * 渲染首页模板
     */
    public String renderColumn(int siteId,int channelId,Map param,boolean preview,String rootPath){
        //获取栏目信息
        PortalColumn portalColumn = portalColumnMapper.selectByPrimaryKey(channelId);
//        Site site = siteMapper.selectByPrimaryKey(siteId);
        //获取站点首页模板信息
        PortalTemplate portalTemplate = portalTemplateMapper.selectByPrimaryKey(portalColumn.getListTemplate());
        if(portalTemplate != null){
            //存在则拼接站点磁盘路径
            String path = rootPath + File.separator + "cmsTmp" + File.separator + portalTemplate.getPortalId();
            Configuration config = new Configuration();
            ByteArrayOutputStream outStream = null;
            Writer w = null;
            try{
                config.setDirectoryForTemplateLoading(new File(path));

                //把service添加到参数去
                //站点Service
                config.setCustomAttribute("siteMapper",siteMapper);
                //站点模板Service
                config.setCustomAttribute("portalTemplateMapper",portalTemplateMapper);
                //栏目Mapper
                config.setCustomAttribute("portalColumnMapper",portalColumnMapper);

                //文章Mapper
                config.setCustomAttribute("portalArticleMapper",portalArticleMapper);
                //渲染的Service
                config.setCustomAttribute("renderingService",renderingService);

                //附加参数
                config.setCustomAttribute("ROOT_PATH","/");
                config.setCustomAttribute("SITE_ID",siteId);
                config.setCustomAttribute("channelId",channelId);

                //预览模式
                config.setCustomAttribute("preview",true);

                Template template = config.getTemplate(portalTemplate.getTemplateFile(),"UTF-8");
                outStream = new ByteArrayOutputStream();
                w = new OutputStreamWriter(outStream,"UTF-8");

                //设置参数
                    template.process(CMSTemplateUtil.getInstance(),w);
                return outStream.toString("UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }finally {
                if(outStream!=null){
                    try {
                        outStream.close();
                    } catch (IOException e) {
                    }
                }
                if(w!=null){
                    try {
                        w.close();
                    } catch (IOException e) {

                    }
                }
            }
        }

        return null;
    }

    /**
     * 渲染文章页面
     */
    public String renderArticle(int siteId,int channelId,int articleId,Map param,boolean preview,String rootPath){
        //获取栏目信息
        PortalColumn portalColumn = portalColumnMapper.selectByPrimaryKey(channelId);
        //获取站点首页模板信息
        PortalTemplate portalTemplate = portalTemplateMapper.selectByPrimaryKey(portalColumn.getDetailTemplate());
        if(portalTemplate != null){
            //存在则拼接站点磁盘路径
            String path = rootPath + File.separator + "cmsTmp" + File.separator + portalTemplate.getPortalId();
            Configuration config = new Configuration();
            ByteArrayOutputStream outStream = null;
            Writer w = null;
            try{
                config.setDirectoryForTemplateLoading(new File(path));

                //把service添加到参数去
                //站点Service
                config.setCustomAttribute("siteMapper",siteMapper);
                //站点模板Service
                config.setCustomAttribute("portalTemplateMapper",portalTemplateMapper);
                //栏目Mapper
                config.setCustomAttribute("portalColumnMapper",portalColumnMapper);

                //文章Mapper
                config.setCustomAttribute("portalArticleMapper",portalArticleMapper);
                //渲染的Service
                config.setCustomAttribute("renderingService",renderingService);

                //附加参数
                config.setCustomAttribute("ROOT_PATH","/");
                config.setCustomAttribute("SITE_ID",siteId);
                config.setCustomAttribute("channelId",channelId);
                config.setCustomAttribute("articleId",articleId);

                //预览模式
                config.setCustomAttribute("preview",true);

                Template template = config.getTemplate(portalTemplate.getTemplateFile(),"UTF-8");
                outStream = new ByteArrayOutputStream();
                w = new OutputStreamWriter(outStream,"UTF-8");

                //设置参数
                template.process(CMSTemplateUtil.getInstance(),w);
                return outStream.toString("UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }finally {
                if(outStream!=null){
                    try {
                        outStream.close();
                    } catch (IOException e) {
                    }
                }
                if(w!=null){
                    try {
                        w.close();
                    } catch (IOException e) {

                    }
                }
            }
        }

        return null;
    }


    //根据栏目标识串获取到 栏目下的文章
    public List<PortalArticleWithBLOBs> getDocByChannelId(int columnId, int start, int size, String orderBy){
        Map param = new HashMap();
        param.put("columnId",columnId);
        param.put("start",start);
        param.put("size",size);
        if(orderBy!=null && !"".equals(orderBy)){
            param.put("orderBy",orderBy);
        }
       return portalArticleMapper.getDocByChannelId(param);
    }

    /**
     * 获取站点的完整发布路径
     * 不断遍历父栏目，获取标识，最后添加站点标识
     */

    public String getCloumnAbsolutePath(int columnId){
        StringBuffer path = new StringBuffer();
        PortalColumn p = portalColumnMapper.selectByPrimaryKey(columnId);
        path.insert(0,"/"+p.getColumnMark());
        while(p.getParentColumnId()!=0){
            p = portalColumnMapper.selectByPrimaryKey(p.getParentColumnId());
            path.insert(0,"/"+p.getColumnMark());
        }
        Site s = siteMapper.selectByPrimaryKey(p.getPortalId());
        path.insert(0,s.getPortalMark());
        return path.toString();
    }

}
